package com.ld.zxw.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.JsonKit;
import com.ld.zxw.Documents.Documents;
import com.ld.zxw.config.Config;
import com.ld.zxw.config.DefaultObject;
import com.ld.zxw.config.LuceneDataSource;

public class CommonUtil {

	private static Logger log = Logger.getLogger(CommonUtil.class);

	public synchronized static IndexWriter getIndexWriter(Config config){
		IndexWriterConfig conf = new IndexWriterConfig(config.getAnalyzer());
		try {
			return new IndexWriter(config.getDirectory(), conf);
		} catch (IOException e) {
			log.error("获取 IndexWriter error:",e);
			return null;
		}
	}

	public synchronized static void colseIndexWriter(IndexWriter writer){
		if(writer != null){
			try {
				writer.close();
			} catch (IOException e) {
				log.error("关闭流异常:",e);
			}
		}
	}


	public synchronized static List<Document> ObjOrDoc(List<Object> objs,Config config, String defaultKey){
		List<Document> documents = new ArrayList<>();
		List<DefaultObject> xmlDem = config.getXmlDem();
		List<BoostDto> boosts = getBoosts();
		int size = objs.size();
		for (int i = 0; i < size; i++) {
			String json = JsonKit.toJson(objs.get(i));
			documents.add(getDocument(JSON.parseObject(json), xmlDem, boosts));
		}
		return documents;

	}

	public synchronized static List<Document> mapOrdoc(@SuppressWarnings("rawtypes") List<HashMap> list,Config config, String defaultKey){
		List<Document> documents = new ArrayList<>();
		List<DefaultObject> xmlDem = config.getXmlDem();
		List<BoostDto> boosts = getBoosts();
		int size = list.size();
		for (int i = 0; i < size; i++) {
			documents.add(getDocument(list.get(i), xmlDem, boosts));
		}
		return documents;
	}
	
	public static Document getDocument(JSONObject obj,List<DefaultObject> xmlDem,List<BoostDto> boosts){
		Documents doc = new Documents(boosts);
		int size_ = xmlDem.size();
		for (int j = 0; j < size_; j++) {
			DefaultObject defaultObject = xmlDem.get(j);
			String name = defaultObject.getName();
			doc.put(name, obj.get(name),defaultObject.getType(),defaultObject.getBoost());
		}
		return doc.getDocument();
	}
	public static Document getDocument(HashMap obj,List<DefaultObject> xmlDem,List<BoostDto> boosts){
		Documents doc = new Documents(boosts);
		int size_ = xmlDem.size();
		for (int j = 0; j < size_; j++) {
			DefaultObject defaultObject = xmlDem.get(j);
			String name = defaultObject.getName();
			doc.put(name, obj.get(name),defaultObject.getType(),defaultObject.getBoost());
		}
		return doc.getDocument();
	}

	/**
	 * 过滤非法字符
	 * @param str 元数据
	 * @param filter 非法数据
	 * @return
	 */
	public synchronized static String IllegalFiltering(String str,String filter){
		if(StringUtils.isEmpty(filter)){
			return str;
		}else{
			char[] data = getChar(str);
			char[] fiter = getChar(filter);
			String key="";
			for (int i = 0; i < data.length; i++) {
				if(isFlag(fiter, data[i])){
					key+=String.valueOf(data[i]);
				}
			}
			return key;
		}
	}
	private static char[] getChar(String str){
		char[] c=new char[str.length()];
		c=str.toCharArray();
		return c;
	}
	public static boolean isFlag(char[] fiter,char vue){
		boolean flag = true;
		for (int j = 0; j < fiter.length; j++) {
			if(fiter[j] == vue){
				flag=false;
				break;
			}
		}
		return flag;
	}

	public static List<BoostDto> getBoosts(){
		if(LuceneDataSource.jedis == null) {
			return null;
		}else {
			List<String> lrange = LuceneDataSource.jedis.lrange("LuceneBoost_LD", 0, Integer.MAX_VALUE);
			return orObj(lrange);
		}
	}
	public static List<BoostDto> orObj(List<String> list){
		List<BoostDto> dtos = new ArrayList<>();
		int size = list.size();
		for (int i = 0; i < size; i++) {
			dtos.add(JSONObject.parseObject(list.get(i), BoostDto.class));
		}
		return dtos;

	}
}
